package dk.mga.androidExplore.gpsTracker;

import java.util.ArrayList;
import java.util.Iterator;

import dk.mga.androidExplore.R;
import dk.mga.androidExplore.SensorView;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GPSRecordingDetails extends Activity {
	
	private static final String tag  = "myLog";
	private String recordingName;
	private DBHandler dbh;
	private SensorView profileView;
	private int sampleCount;
	private float maxSpeed;
	private int duration;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		recordingName = getIntent().getStringExtra(GPSTracker.RECORDING_NAME);
		setContentView(R.layout.gps_recording_detail);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		dbh = DBHandler.singleton(this);
		TextView tv = (TextView) findViewById(R.id.gps_detail_startTime_id);
		tv.setText("Start time:"+DateFormat.format("dd MMMM kk:mm:ss yyyy", dbh.getStartTime(recordingName)));
		tv = (TextView) findViewById(R.id.gps_detail_maxSpeed_id);
		maxSpeed = dbh.getMaxSpeedKmH(recordingName);
		tv.setText("Max speed:"+ maxSpeed);
		tv = (TextView) findViewById(R.id.gps_detail_duration_id);
		duration = dbh.getDurationInSec(recordingName);
		tv.setText("Duration: "+duration/3600+":"+(duration%3600)/60+":"+duration%60);
		tv = (TextView) findViewById(R.id.gps_detail_samples_id);
		sampleCount = dbh.getSamplesCounts(recordingName);
		tv.setText("Samples # "+sampleCount);
		tv = (TextView) findViewById(R.id.gps_detail_headline_id);
		tv.setText("" + recordingName);
		tv = (TextView) findViewById(R.id.gps_detail_altitude_id);
		tv.setText("Max altitude "+dbh.getMaxAltitude(recordingName));
		tv = (TextView) findViewById(R.id.gps_detail_accuracy_id);
		tv.setText("Worst (max) accuracy "+dbh.getMaxAccuracy(recordingName));
		LinearLayout ll = (LinearLayout) findViewById(R.id.gps_detail_profile);
		profileView = new SensorView(this);
		ll.addView(profileView);
		handler.postDelayed(delayedJob, 1);// this delay gives time to finish layout..
	}

	Handler handler = new Handler();
	int notForever = 10;
	
	Runnable delayedJob = new Runnable() {
		@Override
		public void run() {
			// getXresolution() returns 0 until layout is finished
			if (profileView.getXresolution() == 0 && notForever-- > 0) {
				Log.d(tag, "Waiting for View - but not forever #"+notForever);
				handler.postDelayed(delayedJob, 1);
			} else {
				showProfile();
			}
		}
	};

	/**
	 * Finds scale factor and scale values
	 * to fit in view.
	 */
	private void showProfile() {
		Iterator<Location> i = dbh.getLocations(recordingName);
		Log.d(tag, "xResolution="+profileView.getXresolution()
				+"\nyResolution="+profileView.getYresolution()
				+"\nMaxSpeed="+Float.toString(maxSpeed)
				+"\nsamples="+sampleCount);
		float scaleFactorY = (maxSpeed==0)?1f:((float)profileView.getYresolution())/maxSpeed;
		float scaleFactorX = (duration==0)?1f:((float)profileView.getXresolution())/duration;
		Log.d(tag, "x:y Scale = "+Float.toString(scaleFactorX)+":"+Float.toString(scaleFactorY));
		boolean firstSample = true;
		long start=0;
		ArrayList<SensorView.Point> points = new ArrayList<SensorView.Point>();
		while (i.hasNext()) {
			Location l = i.next();
			if (firstSample) {
				firstSample=false;
				start = l.getTime()/1000;// calculating in seconds
			}
			long x = (l.getTime()/1000-start)*profileView.getXresolution();
			int xInt,yInt;
			xInt = (int)(x/duration);
			yInt = (int)(DBHandler.fromMpStoKMpH(l.getSpeed())*scaleFactorY) - profileView.getYresolution();
			points.add(profileView.new Point(xInt, yInt));
		}
		profileView.setPoints(points);
	}

}
